package com.woniuxy.oldmanapi.service;

import com.woniuxy.oldmanapi.entity.Bed;
import com.woniuxy.oldmanapi.entity.Building;
import com.woniuxy.oldmanapi.entity.Room;
import com.woniuxy.oldmanapi.entity.Storey;
import com.woniuxy.oldmanapi.mapper.BedMapper;
import com.woniuxy.oldmanapi.mapper.BuildingMapper;
import com.woniuxy.oldmanapi.mapper.RoomMapper;
import com.woniuxy.oldmanapi.mapper.StoreyMapper;
import com.woniuxy.oldmanapi.vo.PiliangVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PiliangService {

    @Autowired
    StoreyMapper storeyMapper;
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    BedMapper bedMapper;

    public void add(PiliangVo piliangVo) {
        Building building = new Building();
        building.setBuildingname(piliangVo.getBuildingName());
        buildingMapper.insert(building);
        int bid = building.getId();
        for (int i = 1; i <= piliangVo.getStoreyNum(); i++) {
            Storey storey = new Storey();
            String sname;
            if (i < 10) {
                sname = piliangVo.getStoreyNamePrefix() + '0' + i;
            } else {
                sname = piliangVo.getStoreyNamePrefix() + i;
            }
            storey.setStoreyname(sname);
            storey.setBuildingId(bid);
            storeyMapper.insert(storey);
            int sid = storey.getId();
            for (int j = 1; j <= piliangVo.getRoomNum(); j++) {
                Room room = new Room();
                String rname;
                if (j < 10) {
                    rname = piliangVo.getRoomNamePrefix() + '0' + j;
                } else {
                    rname = piliangVo.getRoomNamePrefix() + j;
                }
                room.setBuildingId(bid);
                room.setHousetypeId(piliangVo.getHousetypeId());
                room.setRoomname(rname);
                room.setStoreyId(sid);
                roomMapper.insert(room);
                int rid = room.getId();
                for (int k = 1; k <= piliangVo.getBedNum(); k++) {
                    Bed bed = new Bed();
                    String bname = piliangVo.getBuildingName() + '-'+ sname + '-' + rname + '-' + "床位" + k;
                    bed.setState(0);
                    bed.setBuidingId(bid);
                    bed.setStoreyId(sid);
                    bed.setRoomId(rid);
                    bed.setBedname(bname);
                    bedMapper.insert(bed);
                }
            }
        }
    }

}
